import flet as ft
from app import VideoDownloaderApp

def main(page: ft.Page):
    # 设置页面属性
    page.title = "视频下载器"
    page.theme_mode = ft.ThemeMode.LIGHT
    page.window_width = 800
    page.window_height = 600
    page.window_resizable = False
    page.window_center()
    
    # 创建应用实例
    app = VideoDownloaderApp()
    app.page = page
    
    # 创建导航栏
    rail = ft.NavigationRail(
        selected_index=0,
        label_type=ft.NavigationRailLabelType.ALL,
        extended=True,
        destinations=[
            ft.NavigationRailDestination(
                icon=ft.icons.HOME_OUTLINED,
                selected_icon=ft.icons.HOME,
                label="主页"
            ),
            ft.NavigationRailDestination(
                icon=ft.icons.DOWNLOAD_OUTLINED,
                selected_icon=ft.icons.DOWNLOAD,
                label="下载列表"
            ),
            ft.NavigationRailDestination(
                icon=ft.icons.SETTINGS_OUTLINED,
                selected_icon=ft.icons.SETTINGS,
                label="设置"
            ),
        ],
        on_change=lambda e: handle_route_change(e, app, page)
    )
    
    # 创建主容器
    container = ft.Container(
        content=ft.Row([
            rail,
            ft.VerticalDivider(width=1),
            ft.Column([app.create_home_page()], expand=True),
        ], expand=True),
    )
    
    # 设置页面内容
    page.add(container)
    
    # 初始化路由
    page.go('/')

def handle_route_change(e, app, page):
    """处理路由变化"""
    index = e.control.selected_index
    if index == 0:
        page.go('/')
        page.views[-1].controls[1].content.controls[1] = app.create_home_page()
    elif index == 1:
        page.go('/downloads')
        page.views[-1].controls[1].content.controls[1] = app.create_download_page()
    else:
        page.go('/settings')
        page.views[-1].controls[1].content.controls[1] = app.create_settings_page()
    page.update()

if __name__ == '__main__':
    ft.app(target=main) 